import React, { Component } from "react";

class App extends Component {
  state = {
    value: "",
    areaValue: "",
    selectValue: "2",
    radioValue: "",
    list: [],
  };

  handleChange1 = (e) => {
    this.setState({
      value: e.target.value,
    });
  };
  handleChange2 = (e) => {
    this.setState({
      areaValue: e.target.value,
    });
  };
  handleChange3 = (e) => {
    this.setState({
      selectValue: e.target.value,
    });
  };

  handleChange4 = (e) => {
    this.setState({
      radioValue: e.target.value,
    });
  };

  handleChange5 = (e) => {
    // console.log(e.target.checked);
    if (e.target.checked) {
      this.state.list.push(e.target.value);
      this.setState({});
    } else {
      const newArr = this.state.list.filter((item) => item !== e.target.value);
      this.setState({
        list: newArr,
      });
    }
  };

  handleClick = () => {
    console.log({
      value: this.state.value,
      areaValue: this.state.areaValue,
      selectValue: this.state.selectValue,
      radioValue: this.state.radioValue,
      list: this.state.list,
    });
  };

  render() {
    return (
      <>
        <h2>表单</h2>
        <h3>普通输入框</h3>
        <input
          type="text"
          value={this.state.value}
          onChange={this.handleChange1}
        />
        <hr />
        <h3>多行文本框</h3>
        <textarea
          value={this.state.areaValue}
          onChange={this.handleChange2}
        ></textarea>
        <hr />
        <select value={this.state.selectValue} onChange={this.handleChange3}>
          <option value="1">男</option>
          <option value="2">女</option>
          <option value="3">未知</option>
        </select>
        <hr />
        <input
          type="radio"
          name="sex"
          value="a"
          onChange={this.handleChange4}
        />
        男
        <input
          type="radio"
          name="sex"
          value="b"
          onChange={this.handleChange4}
        />
        女
        <hr />
        <input type="checkbox" value="c" onChange={this.handleChange5} />唱
        <input type="checkbox" value="t" onChange={this.handleChange5} />跳
        <input type="checkbox" value="r" onChange={this.handleChange5} />
        rap
        <input type="checkbox" value="l" onChange={this.handleChange5} />
        篮球
        <hr />
        <button onClick={this.handleClick}>btn</button>
      </>
    );
  }
}

export default App;
